//
//  KSKAAlertViewController.m
//  DashboardHD
//
//  Created by 楊亜玲 on 17/3/23.
//  Copyright © 2017年 Totyu. All rights reserved.
//

#import "KSKAAlertViewController.h"
#import "KSKAHomeTopItem.h"
#import "KSKACollectionViewHorizontalLayout.h"
#import "KSKAMainTool.h"
#import "KSKARoomCell.h"

@interface KSKAAlertViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;

/** 房间信息模型数组 */
@property (nonatomic, strong) NSArray *itemArray;
/** 页数 */
@property(nonatomic, assign) NSUInteger pageCount;

@end

@implementation KSKAAlertViewController

static NSString * const reuseRoomCellId = @"KSKARoomCell";
static NSString * const reuseIdentifierForWhiteCell = @"KSKARoomWhiteCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1.导航栏按钮
    [self setupLeftNavi];
    [self setupRightNavi];
    
    // 2.设置collectionView
    [self setupCollectionView];

    // 请求数据
    [self custList];
    
}

#pragma mark - 导航栏按钮

-(void)setupLeftNavi{
    KSKAHomeTopItem *logoutItem = [KSKAHomeTopItem item];
    logoutItem.title = @"ログアウト";
    logoutItem.btnColor = [UIColor orangeColor];
    [logoutItem addTarget:self action:@selector(logoutClick)];
    UIBarButtonItem *logoutTopItem = [[UIBarButtonItem alloc]initWithCustomView:logoutItem];
    self.navigationItem.leftBarButtonItem = logoutTopItem;
}

-(void)setupRightNavi{
    KSKAHomeTopItem *helpItem = [KSKAHomeTopItem item];
    helpItem.title = @"ヘルプ";
    helpItem.btnColor = KSKAMainColor;
    [helpItem addTarget:self action:@selector(helpClick)];
    UIBarButtonItem *helpTopItem = [[UIBarButtonItem alloc]initWithCustomView:helpItem];
    
    KSKAHomeTopItem *noticeItem = [KSKAHomeTopItem item];
    noticeItem.title = @"お知らせ";
    noticeItem.btnColor = KSKAMainColor;
    [noticeItem addTarget:self action:@selector(noticeClick)];
    UIBarButtonItem *noticeTopItem = [[UIBarButtonItem alloc]initWithCustomView:noticeItem];
    
    KSKAHomeTopItem *settingItem = [KSKAHomeTopItem item];
    settingItem.title = @"可視化設定";
    settingItem.btnColor = KSKAMainColor;
    [settingItem addTarget:self action:@selector(settingClick)];
    UIBarButtonItem *settingTopItem = [[UIBarButtonItem alloc]initWithCustomView:settingItem];
    
    
    self.navigationItem.rightBarButtonItems = @[helpTopItem,noticeTopItem,settingTopItem];
}

#pragma mark - 导航栏按钮点击事件
-(void)logoutClick{
    KSKLog(@"logoutClick");
}

-(void)helpClick{
    KSKLog(@"helpClick");
}

-(void)noticeClick{
    KSKLog(@"noticeClick");
}

-(void)settingClick{
    KSKLog(@"settingClick");
}


#pragma mark - 设置CollectionView
-(void)setupCollectionView{
    // 1.布局
    KSKACollectionViewHorizontalLayout *layout =[[KSKACollectionViewHorizontalLayout alloc]init];
    layout.itemCountPerRow = xRowNum;
    layout.rowCount = yRowNum;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView.collectionViewLayout = layout;

    // 2.注册重用空白cell类(从storyboarb创建不需要注册)
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifierForWhiteCell];
}

#pragma mark - 设置PageControl
-(void)setupPageControl{
    _pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    _pageControl.currentPageIndicatorTintColor = KSKAMainColor;
    
}

#pragma mark - 网络请求事件
-(void)custList{
    KSKAMainParam *param = [[KSKAMainParam alloc]init];
    param.buildingid = @"2";
    param.floorno = @"3";
    [KSKAMainTool custListWithParam:param success:^(NSArray *array) {
        if (array.count > 0 ) {
            // 1.取得模型数组
            self.itemArray = array;
            // 2.根据模型数组，计算页数
            self.pageCount = _itemArray.count;
            while (_pageCount % (xRowNum * yRowNum) != 0) {
                ++_pageCount;
            }
            // 3.根据页数，设置pageControl
            [self setupPageControl];
            self.pageControl.numberOfPages = _pageCount / (xRowNum * yRowNum);
            // 4.刷新collectionView
            [self.collectionView reloadData];
        }else{
#warning 提示没有获取到房间信息，
        }
       
    } failure:^(NSError *error) {
#warning  提示网络请求失败
    }];
}


#pragma mark - UICollectionViewDataSource

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.pageCount;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake([UIScreen mainScreen].bounds.size.width  / xRowNum ,self.collectionView.frame.size.height / yRowNum);
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    // 创建空白cell
    if (indexPath.item >= self.itemArray.count) {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifierForWhiteCell
                                                         forIndexPath:indexPath];
        return cell;
    }
    // 1.创建cell
    else {
        KSKARoomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseRoomCellId
                                                         forIndexPath:indexPath];
        // 2.传递数据模型
        KSKAMainResult *roomInfo = [[KSKAMainResult alloc]init];
        roomInfo = [self.itemArray objectAtIndex:indexPath.item];
        cell.room = roomInfo;
        return cell;
    }
}

/*
//当scrollView正在滚动就会调用
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //根据滚动位置决定pageControl显示第几页
    
    CGFloat scrollW = self.collectionView.frame.size.width;
    int page = (scrollView.contentOffset.x + scrollW * 0.5)/scrollW;
    self.pageControl.currentPage = page;
    NSLog(@"page:%d",page);
    
}
*/




@end
